package com.guaoran.jvm;

import java.util.Date;

/**
 * @author : guaoran
 * @Description
 *  Try final return 返回的结果输出
 * @date :2019/3/2 20:28
 */
public class TryFinalDemo {

    public static void main(String[] args) throws InterruptedException {
        System.out.println(demo());
        System.out.println(demo2());
        System.out.println(demo3());
    }

    /**
     * 字符串和基本类型是已经将数据放到内存中了，return的时候，直接是用内存中取得
     * @return
     */
    public static String demo(){
        String str = "hello";
        try {
            return str;
        }finally {
            str = "world";
        }
    }
    public static Integer demo2(){
        Integer i = 2222;
        try {
            return i;
        }finally {
            i=11111;
        }
    }

    /**
     * 对象的是修改地址的引用
     * @return
     * @throws InterruptedException
     */
    public static User demo3() throws InterruptedException {
        User user = new User();
        user.setId(1L);
        user.setAge(18);
        user.setName("zs");
        Date date = new Date();
        System.out.println("date="+date);
        user.setCreateTime(date);
        try {
            return user;
        }finally {
            user.setId(2L);
            user.setAge(28);
            user.setName("222zs");
            Thread.sleep(2000);
            Date date2 = new Date();
            System.out.println("date2="+date2);
            user.setCreateTime(date2);
        }
    }
}
